/*
* 1videoConference -- An open source video conferencing platform.
*
* Copyright (C) 2007 - 2008, Adiance Technologies Pvt. Ltd.
*
* Hardik Sanghvi <hardik.sanghvi @adiance.com>
*
* See http://www.1videoconference.org for more information about
* the 1videoConference project. Please do not directly contact
* any of the maintainers of this project for assistance;
* the project provides a web site, forums and mailing lists
* for your use.
*
* This program is free software, distributed under the terms of
* the GNU General Public License Version 2. See the LICENSE file
* at the top of the source tree.
*/

using System;
using System.Collections.Generic;
using System.Text;
using System.ServiceModel;
using System.IO;

using System.Data;
using System.Xml;
using System.Collections;

namespace Adiance.VMukti.BAL
{
    [ServiceContract()]
    public interface IsvcFileTransfer
    {
        [OperationContract(IsOneWay = true)]
        void svcSetFile(string fName);

        [OperationContract(IsOneWay = true)]
        void svcSendFile(byte[] pptFile);

        [OperationContract(IsOneWay = true)]
        void svcSaveFile();

        [OperationContract(IsOneWay = true)]
        void svcDeleteFolder(string fname);
    }

    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)]
    public class CsvcFileTransfer : IsvcFileTransfer
    {
        System.IO.FileStream fs;
        public static XmlDocument xmldoc = new XmlDocument();

        string CurrDir = "";
        string LocalPath = "";
        System.Reflection.Assembly ass = System.Reflection.Assembly.GetAssembly(typeof(ZipUnzip));
        ZipUnzip z = new ZipUnzip();
        public static ArrayList alAllFiles = new ArrayList();

        public CsvcFileTransfer()
        {
            LocalPath = ass.Location.Replace("Adiance.VMukti.BAL.dll", @"Zip Files");
        }

        public void svcSetFile(string fName)
        {
            try
            {
                fs = File.Open(LocalPath + @"\" + fName.Split('\\')[fName.Split('\\').Length - 1], FileMode.OpenOrCreate, FileAccess.ReadWrite);
            }
            catch (Exception exp)
            {
                exp.Data.Add("My Key", "Adiance.VMukti.BAL--:--svcFileTransfer.cs--:--svcSetFile()--:--");
                Adiance.VMukti.BAL.clsUtility.LogException = exp;
                //System.Windows.MessageBox.Show("svcSetFile=" + exp.Message);
            }
        }

        public void svcSendFile(byte[] pptFile)
        {
            try
            {
                fs.Write(pptFile, 0, pptFile.Length);
            }
            catch (Exception exp)
            {
                exp.Data.Add("My Key", "Adiance.VMukti.BAL--:--svcFileTransfer.cs--:--svcSendFile()--:--");
                Adiance.VMukti.BAL.clsUtility.LogException = exp;
                //System.Windows.MessageBox.Show("svcSendFile" + exp.Message);
            }
        }

        public void svcSaveFile()
        {
            try
            {
                string mName = fs.Name;
                fs.Close();
                fs.Dispose();
                z.uploadfile(mName, mName.Split('\\')[mName.Split('\\').Length - 1].Replace(".zip", ""));               
            }
            catch (Exception exp)
            {
                exp.Data.Add("My Key", "Adiance.VMukti.BAL--:--svcFileTransfer.cs--:--svcSaveFile()--:--");
                Adiance.VMukti.BAL.clsUtility.LogException = exp;
                //System.Windows.MessageBox.Show("svsSaveFile" + exp.Message);
            }
        }

        public void svcDeleteFolder(string fname)
        {
            try
            {
                string pathview = ass.Location.Replace("Adiance.VMukti.BAL.dll", @"Presentation Layer");
                string pathdal = ass.Location.Replace("Adiance.VMukti.BAL.dll", @"Data Layer");
                string pathbal = ass.Location.Replace("Adiance.VMukti.BAL.dll", @"Business Layer");

                if (File.Exists(LocalPath + "\\" + fname + ".zip"))
                {
                    File.Delete(LocalPath + "\\" + fname + ".zip");
                }
                if (Directory.Exists(pathview + "\\" + fname))
                {
                    Directory.Delete(pathview + "\\" + fname, true);
                }
                if (Directory.Exists(pathdal + "\\" + fname))
                {
                    Directory.Delete(pathdal + "\\" + fname, true);
                }
                if (Directory.Exists(pathbal + "\\" + fname))
                {
                    Directory.Delete(pathbal + "\\" + fname, true);
                }
            }
            catch (Exception exp)
            {
                exp.Data.Add("My Key", "Adiance.VMukti.BAL--:--svcFileTransfer.cs--:--svcDeleteFolder()--:--");
                Adiance.VMukti.BAL.clsUtility.LogException = exp;
                //System.Windows.MessageBox.Show("svcDeleteFolder:" + exp.Message);
            }
        }
    }
}
